package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.CompanyModel;

/**
 * 公司查询条件
 * 作者：柳伟杰 on 2016/3/21 16:22
 * 邮箱：1031066280@qq.com
 */
public class SearchCompanyActivity extends BaseActivity {
    @CodeNote(id = R.id.toolbar_center_et)
    EditText toolbar_center_et;
    @CodeNote(id = R.id.company_search_list)
    ListView company_search_list;
    @CodeNote(id = R.id.search_company_ll)
    LinearLayout search_company_ll;
    @CodeNote(id = R.id.toolbar_right_ll, click = "onClick")
    LinearLayout toolbar_right_ll;
    @CodeNote(id = R.id.toolbar_left_ll, click = "onClick")
    LinearLayout toolbar_left_ll;
    CommonAdapter mAdapter;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_search_company);
    }

    @Override
    public void initEvents() {
        toolbar_center_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String choice = toolbar_center_et.getText().toString().trim();
                if (!("").equals(choice)) {
                    search_company_ll.setVisibility(View.GONE);
                    company_search_list.setVisibility(View.VISIBLE);
                    final List<CompanyModel> list = finalDb.findAllByWhere(CompanyModel.class, "CompanyName like '%" + choice + "%'");
                    mAdapter = new CommonAdapter<CompanyModel>(SearchCompanyActivity.this, list, R.layout.item_company) {
                        @Override
                        public void convert(ViewHolder holder, CompanyModel model, int position) {
                            holder.setText(R.id.company_name_tv, model.getCompanyName());
                        }
                    };
                    company_search_list.setAdapter(mAdapter);
                    company_search_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putString("companyName", list.get(position).getCompanyName());//单位名称
                            intent.putExtras(bundle);
                            setResult(21, intent);
                            SearchCompanyActivity.this.finish();
                        }
                    });
                } else {
                    search_company_ll.setVisibility(View.VISIBLE);
                    company_search_list.setVisibility(View.GONE);
                }
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_right_ll://点击查询触发事件
                break;
            case R.id.toolbar_left_ll://关闭页面
                this.finish();
                break;
        }
    }
}
